package com.heima.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.crm.common.contents.CommonMessageContent;
import com.heima.crm.common.result.PageResult;
import com.heima.crm.common.result.Result;
import com.heima.crm.common.utils.BaseContext;
import com.heima.crm.mapper.CrUserMapper;
import com.heima.crm.mapper.PostMapper;
import com.heima.crm.pojos.dtos.PostPageDTO;
import com.heima.crm.pojos.entity.SysPost;
import com.heima.crm.pojos.entity.TbUser;
import com.heima.crm.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysPostServiceImpl extends ServiceImpl<PostMapper, SysPost> implements PostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private CrUserMapper crUserMapper;

    /**
     * 岗位动态模糊分页条件查询
     * @param postPageDTO
     * @return
     */
    @Override
    public Result postPageList(PostPageDTO postPageDTO) {

        //校验参数
        if (postPageDTO == null){
            return Result.error(CommonMessageContent.INVALID_PARAMETER);
        }

        Integer page1 = postPageDTO.getPage();
        Integer size = postPageDTO.getSize();
        if (page1==null && size == null){
            page1=1;
            size=10;
        }

        //创建一个Lambda对象
        LambdaQueryWrapper<SysPost>lqw=new LambdaQueryWrapper<>();

        //然后判断对象是否为空,如果不为空就进行模糊查询
        if (postPageDTO.getPostName() != null && postPageDTO.getPostName() != ""){
            lqw.like(SysPost::getPostName,postPageDTO.getPostName());
        }
        if (postPageDTO.getCode()!=null){
            lqw.like(SysPost::getCode,postPageDTO.getCode());
        }
        //岗位状态查询
        if (postPageDTO.getStatus() != null){
            lqw.eq(SysPost::getStatus,postPageDTO.getStatus());
        }

        //按照排序字段排序
        lqw.orderByDesc(SysPost::getShowSort);

        //分页查询
        IPage<SysPost>page=new Page<>(page1,size);
        page=page(page,lqw);

        PageResult pageResult=new PageResult(page.getTotal(),page.getRecords());
        return Result.success(pageResult);
    }

    /**
     * 添加岗位
     * @param sysPost
     * @return
     */
    @Override
    public Result addPost(SysPost sysPost) {

        //参数校验
        if (sysPost == null){
            return Result.error(CommonMessageContent.INVALID_PARAMETER);
        }

        if (sysPost.getPostName() != null){
            SysPost sysPost1 = postMapper.selectOne(Wrappers.<SysPost>lambdaQuery().eq(SysPost::getPostName, sysPost.getPostName()));

            if (sysPost1 != null){
                return Result.error(CommonMessageContent.ALREADY_EXISTS);
            }
             postMapper.insert(sysPost);
        }
        return Result.success("添加成功");
    }

    /**
     * 修改岗位
     * @param sysPost
     * @return
     */
    @Override
    public Result updatePost(SysPost sysPost) {

        Long currentId = BaseContext.getCurrentId();
        TbUser tbUser = crUserMapper.selectById(currentId);

        //参数校验
        if (sysPost == null){
            return Result.error(CommonMessageContent.INVALID_PARAMETER);
        }
        sysPost.setUpdateTime(new Date());
        sysPost.setUpdateUser(tbUser.getUserName());

        postMapper.updateById(sysPost);

        return Result.success("修改成功");
    }


    /**
     *删除岗位
     * @param id
     * @return
     */
    @Override
    public Result deletePost(Long id) {

        //校验参数
        if (id == null){
            return Result.error(CommonMessageContent.INVALID_PARAMETER);
        }

        SysPost sysPost = postMapper.selectById(id);
        if (sysPost.getStatus() == 0){
            throw new RuntimeException("岗位启用中,不能删除");
        }
        postMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result startOrStop(Integer status, Long id) {
        SysPost sysPost = postMapper.selectById(id);
        sysPost.setStatus(status);
        postMapper.updateById(sysPost);
        return Result.success("修改成功");
    }


}
